//app.js
const http = require('./http/http.js')
import webSocket from './utils/websocket'
import consultationWebsocket from './utils/consultationWebsocket'
import $service from './service';
App({
  onLaunch: function (e) {
    console.log('小程序打开')
    this.rewriteFinally()
    if ($service.getStore('userInfo')) {
      let userInfo = JSON.parse($service.getStore('userInfo'))
      if ($service.getEnv() != 'develop') {
        if (userInfo.type === 'D') {
          let doctorInfo =  JSON.parse($service.getStore('doctorInfo'))
          if (doctorInfo.isCertification == 0 && $service.isEsgin) {
            wx.clearStorageSync()
          } else {
            wx.redirectTo({
              url: '/pages/doctor_side/index/index',
            })
          }
        }
      }
    }
  },
  onShow(e) {
    wx.setKeepScreenOn({
      keepScreenOn: true,
      complete: (e) => {
        console.log(e)
      }
    })
    console.log(e)
  },
  onUnload:function(){
    console.log("返回")
  },
  onHide:function(){
    console.log("进入了后台")
  },
  // 接口请求
  http,
  // websocket
  webSocket,
  consultationWebsocket,
  // 判断手机型号重写promise.finally
  rewriteFinally() {
    wx.getSystemInfo({
      success: function(res) {
        if (res.platform == 'ios') {
          // 兼容ios真机环境下Promise对象不存在finally方法
          if(!Promise.prototype.finally){
            Promise.prototype.finally = function(callback){
                this.then(res=>{
                    callback&&callback(res)
                },error=>{
                    callback&&callback(error)
                })
            }
          }
        }
      }
    })
  },
  editTabBar: function () {
    //使用getCurrentPages可以获取当前加载中所有的页面对象的一个数组，数组最后一个就是当前页面。
    var curPageArr = getCurrentPages();    //获取加载的页面
    var curPage = curPageArr[curPageArr.length - 1];    //获取当前页面的对象
    var pagePath = curPage.route;    //当前页面url
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }

    var tabBar = this.globalData.tabBar;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;    //根据页面地址设置当前页面状态
      }
    }
    curPage.setData({
      tabBar: tabBar,
      ullist: this.globalData.ullist
    });
  },
  //第二种底部，原理同上
  editTabBar1: function () {
    var curPageArr = getCurrentPages();
    var curPage = curPageArr[curPageArr.length - 1];
    var pagePath = curPage.route;
    if (pagePath.indexOf('/') != 0) {
      pagePath = '/' + pagePath;
    }
    var tabBar = this.globalData.tabBar1;
    for (var i = 0; i < tabBar.list.length; i++) {
      tabBar.list[i].active = false;
      if (tabBar.list[i].pagePath == pagePath) {
        tabBar.list[i].active = true;
      }
    }
    curPage.setData({
      tabBar: tabBar,
      isRead: this.globalData.isRead
    });
  },
  globalData: {
    ullist: [], // 医生候诊人列表
    isRead: 0, // 是否有未读消息 0 无 1有
    isReception: false, // 医生是否在接诊中
    inquiryTime:null,
    inquiryState:"1",
    doctorId:"",
    //第一种底部导航栏显示
    tabBar: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/doctor_side/index/index",
          "text": "首页",
          "iconPath": "/static/images/index_photo/icon_home.png",
          "selectedIconPath": "/static/images/index_photo/icon_home_select.png",
          "clas": "menu-item",
          "selectedColor": "#2b73cc",
          active: true
        },
        {
          "pagePath": "/pages/doctor_side/waiting_list/index",
          "text": "候诊人",
          "iconPath": "/static/images/index_photo/icon_waiting.png",
          "selectedIconPath": "/static/images/index_photo/icon_waiting_select.png",
          "selectedColor": "#2b73cc",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/doctor_side/my_users/my_users",
          "text": "我的用户",
          "iconPath": "/static/images/wdhz_icon.png",
          "selectedIconPath": "/static/images/wdhz_iconA.png",
          "selectedColor": "#2b73cc",
          "clas": "menu-item",
          active: false
        },
        {
          "pagePath": "/pages/doctor_side/personal/personal",
          "text": "个人中心",
          "iconPath": "/static/images/index_photo/icon_my.png",
          "selectedIconPath": "/static/images/index_photo/icon_my_select.png",
          "selectedColor": "#2b73cc",
          "clas": "menu-item",
          active: false
        }
      ],
      "position": "bottom"
    },
    //第二种底部导航栏显示
    tabBar1: {
      "color": "#9E9E9E",
      "selectedColor": "#f00",
      "backgroundColor": "#fff",
      "borderStyle": "#ccc",
      "list": [
        {
          "pagePath": "/pages/user_index/user_index",
          "text": "首页",
          "iconPath": "/static/images/index_photo/icon_home.png",
          "selectedIconPath": "/static/images/index_photo/icon_home_select.png",
          "clas": "menu-item1",
          "selectedColor": "#2b73cc",
          active: false
        },
        {
          "pagePath": "/pages/user_side/inquiry_service/inquiry_service",
          "text": "问诊服务",
          "iconPath": "/static/images/index_photo/icon_outpatient_department.png",
          "selectedIconPath": "/static/images/index_photo/icon_outpatient_department_select.png",
          "selectedColor": "#2b73cc",
          "clas": "menu-item1",
          active: true
        },
        {
          "pagePath": "/pages/user_side/my_doctor/index",
          "text": "我的医生",
          "iconPath": "/static/images/index_photo/icon_doctor.png",
          "selectedIconPath": "/static/images/index_photo/icon_doctor_select.png",
          "selectedColor": "#2b73cc",
          "clas": "menu-item1",
          active: false
        },
        {
          "pagePath": "/pages/user_side/personal/personal",
          "text": "个人中心",
          "iconPath": "/static/images/index_photo/icon_my.png",
          "selectedIconPath": "/static/images/index_photo/icon_my_select.png",
          "selectedColor": "#2b73cc",
          "clas": "menu-item1",
          active: false
        }
      ],
      "position": "bottom"
    }
  }
})
